package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbItemCatMapper;
import com.dongdongshop.model.TbItemCat;
import com.dongdongshop.model.TbItemCatExample;
import com.dongdongshop.service.ItemCatService;
import com.dongdongshop.vo.ItemCatVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ItemCatServiceImpl implements ItemCatService {

    @Autowired
    private TbItemCatMapper tim;

    @Override
    public List<ItemCatVO> catListByParentId(Long parentId) {
        TbItemCatExample example = new TbItemCatExample();
        example.createCriteria().andParentIdEqualTo(parentId);
        List<TbItemCat> catList = tim.selectByExample(example);
        List<ItemCatVO> voList = catList.stream().map(tb -> {
            ItemCatVO itemCatVO = new ItemCatVO();
            BeanUtils.copyProperties(tb, itemCatVO);
            return itemCatVO;
        }).collect(Collectors.toList());
        return voList;
    }

    @Override
    public ItemCatVO tuUpdate(Long id) {
        TbItemCat itemCat = tim.selectByPrimaryKey(id);
        ItemCatVO catVO = new ItemCatVO();
        BeanUtils.copyProperties(itemCat,catVO);
        return catVO;
    }

    @Override
    public void update(ItemCatVO catVO) {
        TbItemCat itemCat = new TbItemCat();
        BeanUtils.copyProperties(catVO,itemCat);
        tim.updateByPrimaryKeySelective(itemCat);
    }

    @Override
    public void add(ItemCatVO catVO) {
        TbItemCat itemCat = new TbItemCat();
        BeanUtils.copyProperties(catVO,itemCat);
        tim.insertSelective(itemCat);
    }

    @Override
    public void delete(Long id) {
        tim.deleteByPrimaryKey(id);
    }
}
